package com.ssyc.gsk_parents.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.ssyc.common.base.BaseApplication;
import com.ssyc.common.http.HttpAdress;
import com.ssyc.common.http.HttpUtils;
import com.ssyc.common.manager.AccountUtils;
import com.ssyc.common.manager.PickChooseManager;
import com.ssyc.common.utils.GsonUtil;
import com.ssyc.common.utils.UiUtils;
import com.ssyc.gsk_parents.R;
import com.ssyc.gsk_parents.activity.ParentsScoreDetailsActivity;
import com.ssyc.gsk_parents.bean.ResultInfo;
import com.ssyc.gsk_parents.bean.ScoreInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

/**
 * 成绩查询列表适配器
 * Created by Administrator on 2018/6/20 0020.
 */

public class ScoreRvAdapter extends BaseQuickAdapter<ScoreInfo.DataBean, BaseViewHolder> {
    private Context context;


    public ScoreRvAdapter(Context context, int layoutResId, @Nullable List<ScoreInfo.DataBean> data) {
        super(layoutResId, data);
        this.context = context;
    }

    @Override
    protected void convert(BaseViewHolder helper, ScoreInfo.DataBean item) {
        //考试的名称
        TextView tvName = helper.getView(R.id.tv_name);
        tvName.setText(item.getTAG());

        //考试的时间
        TextView tvTime = helper.getView(R.id.tv_time);
        tvTime.setText(item.getTIME());

        //查看详情的按钮
        Button button = helper.getView(R.id.bt_details);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                //发送查看成绩的请求
                sendLookDetailsAction(item);

                //点击查看详情跳转到分数详情界面
                Intent intent = new Intent(context, ParentsScoreDetailsActivity.class);
                intent.putExtra("data", item);
                context.startActivity(intent);
            }
        });

        //点评
        TextView tvContent = helper.getView(R.id.tv_content);
        if (TextUtils.isEmpty(item.getCONTENT())) {
            tvContent.setText("暂无点评");
        } else {
            tvContent.setText(item.getCONTENT());
        }

        //展示图片的列表
        RecyclerView rv = helper.getView(R.id.rv);
        //获取试卷图片
        List<String> homeWorkUrls = new ArrayList<>();
        if (!TextUtils.isEmpty(item.getPICTURE1())) {
            homeWorkUrls.add(item.getPIC_URL()+item.getPICTURE1());
        }
        if (!TextUtils.isEmpty(item.getPICTURE2())) {
            homeWorkUrls.add(item.getPIC_URL()+item.getPICTURE2());
        }

        if (!TextUtils.isEmpty(item.getPICTURE3())) {
            homeWorkUrls.add(item.getPIC_URL()+item.getPICTURE3());
        }

        if (!TextUtils.isEmpty(item.getPICTURE4())) {
            homeWorkUrls.add(item.getPIC_URL()+item.getPICTURE4());
        }

        if (!TextUtils.isEmpty(item.getPICTURE5())) {
            homeWorkUrls.add(item.getPIC_URL()+item.getPICTURE5());
        }
        if (!TextUtils.isEmpty(item.getPICTURE6())) {
            homeWorkUrls.add(item.getPIC_URL()+item.getPICTURE6());
        }
        if (!TextUtils.isEmpty(item.getPICTURE7())) {
            homeWorkUrls.add(item.getPIC_URL()+item.getPICTURE7());
        }
        if (!TextUtils.isEmpty(item.getPICTURE8())) {
            homeWorkUrls.add(item.getPIC_URL()+item.getPICTURE8());
        }
        if (!TextUtils.isEmpty(item.getPICTURE9())) {
            homeWorkUrls.add(item.getPIC_URL()+item.getPICTURE9());
        }
        if (homeWorkUrls.size() == 0) {
            //证明没有试卷图片
            rv.setVisibility(View.GONE);
        } else {
            rv.setVisibility(View.VISIBLE);
            LinearLayoutManager manager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
            rv.setLayoutManager(manager);
            rv.setHasFixedSize(true);

            //练习的图片
            RvShowScoreAdapter adapter = new RvShowScoreAdapter(context, R.layout.parent_rv_show_score, homeWorkUrls);
            rv.setAdapter(adapter);
            adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    PickChooseManager.startShowBigActivity(context, homeWorkUrls, position);
                }
            });
        }
    }

    /**
     * 发送查看成绩的请求
     */
    private void sendLookDetailsAction(ScoreInfo.DataBean item) {
        Map<String, String> map = new HashMap<>();
        map.put("type", "52");
        map.put("acc", AccountUtils.getAccount(context));
        map.put("role", AccountUtils.getRole(context));
        map.put("studentId", item.getROW_ID() + "");
        map.put("apptoken", AccountUtils.getToken(context));
        map.put("platform", "8");

        Log.i("test","studentId"+item.getROW_ID());
        HttpUtils.post(HttpAdress.LOOKDETAILS, map, this, new HttpUtils.CallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {
                UiUtils.Toast(BaseApplication.ERROR, false);
            }

            @Override
            public void onResponse(String response, int id) {
                if (!TextUtils.isEmpty(response)) {
                    ResultInfo info = null;
                    try {
                        info = GsonUtil.jsonToBean(response, ResultInfo.class);
                    } catch (Exception e) {
                        Log.i("test", "数据解析异常");
                        UiUtils.Toast("数据解析异常", false);
                    }

                    if (null != info) {
                        if ("200".equals(info.getState())) {
                            //返回成功
                            Log.i("test","查看接口发生成功");
                        } else {
                            //返回失败
                            Log.i("test", "服务器返回异常码是:" + info.getState());
//                            UiUtils.Toast("服务器返回异常,异常码是:" + info.getState(), false);
                        }
                    }
                }
            }
        });
    }
}
